package com.hns.can.base.entity;

import java.io.Serializable;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;

import com.hns.tool.pub.UtilString;


/**
 * 自动生成Id,即将弃用
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
public class UseIdOrGenerate implements IdentifierGenerator{

	@Override
	public Serializable generate(SessionImplementor session, Object obj) throws HibernateException {
	    if (obj == null) throw new HibernateException(new NullPointerException()) ;

	    Serializable id = session.getEntityPersister(null, obj)
                .getClassMetadata().getIdentifier(obj, session);
	    if ( id != null && id.toString().trim().length() > 0 ) {
	    	return id;
	    }
	    return UtilString.getUUID();
	}
}
